/*
 * AJAXCheckAction.java
 *
 * Created on 2007年5月24日, 下午3:33
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.ecomm.chat.action;

import com.ecomm.chat.manager.ChatListenManager;
import com.ecomm.chat.manager.ChatRemindManager;
import com.ecomm.chat.manager.ChatUserInfoManager;
import com.ecomm.platcore.struts.StrutsAction;
import com.ecomm.util.DB;
import com.ecomm.util.DBFactory;
import com.pphome.orgmodel.manager.OrgManager;
import com.pphome.orgmodel.manager.UserManager;
import com.pphome.orgmodel.pojo.UserInfo;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * 主要利用AJAX的 XmlHttpRequest 的方式处理一些信息
 * <p>Title: 易讯聊天系统</p>
 * <p>Description:
 *  易讯聊天系统
 * </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: 联合易讯 </p>
 * @author <font color='blue'>pijunliang</font> &nbsp&nbsp<a href="mailto:pijunliang@hotmail.com">pijunliang@hotmail.com</a>
 * @version 1.0
 * @since 2007年5月24日, 下午3:33
 */
public class AJAXCheckAction  extends StrutsAction {
    /**
     *通过XMLHTTPREQUEST 的方式
     *取出 当前用户的ID ，然后通过输出流的方式
     * 返回 一个 名字
     */
    public ActionForward getName(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PrintWriter out;
        String id = (String)request.getParameter("id");
        if(id == null || id.equals(""))
            return null;
        try {
            this.setNoCacheHeaders(response);
            
            out = response.getWriter();
            String name = getuserNameBYUserid(id);  //查询数据库
            // System.out.println("name:"+name+"--->"+new String(name.getBytes("iso-8859-1"), "UTF-8"));
            out.print(name);//ajax取得都是字符的输出。如果数据量大的话，还可以用xml来发送和接受
            if (out != null) {
                out.flush();
                out.close();
                out = null;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
    /**
     *通过用户ID 得到用户的 中文名字
     */
    public String getuserNameBYUserid(String id){
        UserInfo ui = UserManager.getInstance().getUserInfoById(id);
        if(ui != null && ui.getName() != null){
            return ui.getName();
        }else{
            return null;
        }
    }
    /**
     *封装页面声音提示功能
     *
     *首先用户必须包含 在个人设置中勾上了声音提醒
     *
     *页面的做法是通过AJAX 的方式将 声音控件放入到DIV中
     */
    public ActionForward getChatSound(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PrintWriter out;
        String userid = (String)request.getParameter("userid");
        if(userid == null || userid.equals(""))
            return null;
        try {
            StringBuilder sb = new StringBuilder();
            String count = ChatRemindManager.getInstance().getRemindSound(userid);  //查询数据库
            if(count != null && !count.equals("0") && !count.equals("")){
                
                sb.append("<html><head><title>uecomm</title></head><body>");
                sb.append("<MARQUEE scrollAmount=6 scrollDelay=110 behavior=scroll direction=left>");
                sb.append("<embed src=\"");
                sb.append(request.getContextPath());
                sb.append("/chat/config/2.mid\" hidden=\"true\" LOOP=\"false\" autostart=\"true\" />");
                sb.append("</MARQUEE>");
                sb.append("</body></html>");
            }else{
                sb.append("0");
            }
            this.setNoCacheHeaders(response);
            
            out = response.getWriter();
            //System.out.println("count chat :"+count+"--->");
            out.println(sb.toString());
            if (out != null) {
                out.flush();
                out.close();
                out = null;
            }
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
    /**
     *通过跟CHAT_userinfo 关联 ，取出是否需要弹出一个新窗口，用来提醒用户有多少条新消息
     */
    public ActionForward getChatWindowOpen(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PrintWriter out;
        String userid = (String)request.getParameter("userid");
        if(userid == null || userid.equals(""))
            return null;
        try {
            String count = ChatRemindManager.getInstance().getRemindWindowOpen(userid);  //查询数据库
            this.setNoCacheHeaders(response);
            out = response.getWriter();
            //System.out.println("count chat :"+count+"--->");
            out.print(count);//ajax取得都是字符的输出。如果数据量大的话，还可以用xml来发送和接受
            if (out != null) {
                out.flush();
                out.close();
                out = null;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
    /**
     *图层的方式提醒用户
     *通过跟CHAT_userinfo 关联 ，用来提醒用户有多少条新消息
     */
    public ActionForward getChatCount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PrintWriter out;
        String userid = (String)request.getParameter("userid");
        if(userid == null || userid.equals(""))
            return null;
        try {
            String count = ChatRemindManager.getInstance().getRemindCount(userid);  //查询数据库
            this.setNoCacheHeaders(response);
            out = response.getWriter();
            //System.out.println("count chat :"+count+"--->");
            out.print(count);//ajax取得都是字符的输出。如果数据量大的话，还可以用xml来发送和接受
            if (out != null) {
                out.flush();
                out.close();
                out = null;
                
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
    /**
     *在主页上 显示目前 用户一共有多少为查看消息
     *不用跟CHAT_userinfo 关联 ，用来提醒用户有多少条新消息
     */
    public ActionForward getCount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PrintWriter out;
        String userid = (String)request.getParameter("userid");
        if(userid == null || userid.equals(""))
            return null;
        try {
            String count = ChatRemindManager.getInstance().getCount(userid);  //查询数据库
            this.setNoCacheHeaders(response);
            out = response.getWriter();
            //System.out.println("count chat :"+count+"--->");
            out.print(count);//ajax取得都是字符的输出。如果数据量大的话，还可以用xml来发送和接受
            if (out != null) {
                out.flush();
                out.close();
                out = null;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
    /**
     *通过AJAX的方式校验 用户名字和密码 是否正确
     * 正确就返回 1 ，错误就为 0
     */
    public ActionForward checkloginname(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PrintWriter out;
        try {
            UserInfo ui=(UserInfo)request.getSession().getAttribute("userinfoSess");
            String loginname =ui.getLoginname();
            //String loginname ="";
            String passwordold = (String)request.getParameter("passwordold");
            //System.out.println("passwordold---+"+passwordold);
            boolean bool = ChatUserInfoManager.getInstance().checkuser(loginname,passwordold);  //查询数据库
            //System.out.println("bool---+"+bool);
            String count = "0";
            if(bool){
                count = "1";
            }else{
                count = "0";
            }
            this.setNoCacheHeaders(response);
            out = response.getWriter();
            // System.out.println("count member :"+count+"--->");
            out.print(count);//ajax取得都是字符的输出。如果数据量大的话，还可以用xml来发送和接受
            if (out != null) {
                out.flush();
                out.close();
                out = null;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
    /**
     *修改密码的操作
     *直接得到用户的新密码，然后修改ORMODEL_USERINFO的密码
     */
    public ActionForward changepassword(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PrintWriter out;
        try {
            UserInfo ui=(UserInfo)request.getSession().getAttribute("userinfoSess");
            String loginname =ui.getLoginname();
            //String loginname ="";
            String passwordold = (String)request.getParameter("passwordold");
            String password1 = (String)request.getParameter("password1");
            //System.out.println("passwordold---+"+passwordold);
            String count = "0";  //查询数据库
            if(ChatUserInfoManager.getInstance().checkuser(loginname,passwordold)){
                count =  ChatUserInfoManager.getInstance().changepassword(loginname,password1)+"";
            }else{
                count = "0";
            }
            this.setNoCacheHeaders(response);
            out = response.getWriter();
            // System.out.println("count member :"+count+"--->");
            out.print(count);//ajax取得都是字符的输出。如果数据量大的话，还可以用xml来发送和接受
            if (out != null) {
                out.flush();
                out.close();
                out = null;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
    /**
     *得到目前有多少个人在线的状态
     */
    public ActionForward getMemberCount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PrintWriter out;
        try {
            String count = ChatUserInfoManager.getInstance().getMemberCount("1");  //查询数据库
            this.setNoCacheHeaders(response);
            out = response.getWriter();
            // System.out.println("count member :"+count+"--->");
            out.print(count);//ajax取得都是字符的输出。如果数据量大的话，还可以用xml来发送和接受
            if (out != null) {
                out.flush();
                out.close();
                out = null;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
    /**
     *更新系统的状态
     */
    public ActionForward updateSysState(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        String userid = request.getParameter("userid");
        String state = request.getParameter("state");
        ChatUserInfoManager.getInstance().updateChatUserInfoOnlineStateS(userid,state);  //查询数据库
        return null;
    }
    /**
     *更新监听加入人的名字
     *
     */
    public ActionForward updateListenJionName(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        String chatListenid = request.getParameter("chatListenid");
        String userids = request.getParameter("userids");
        ChatListenManager.getInstance().updateListenJionName(chatListenid,userids);
        return null;
    }
    /**
     *
     *删除监听加入人的名字
     */
    public ActionForward deleteListenJionName(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        if(id != null  && !id.equals("") && id.indexOf("chat") >= 0){
            ChatListenManager.getInstance().deleteListenJionName(id);
        }
        return null;
    }
    /**
     *修改离开PUSHLET的人
     *就是把 当前实例中的人清空
     */
    public ActionForward updateJionPushlet(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        UserInfo ui=(UserInfo)request.getSession().getAttribute("userinfoSess");
        String sendUserid =ui.getId();
        if(id != null  && !id.equals("") && id.indexOf("chat") >= 0){
            ChatListenManager.getInstance().LeavePushlet(id,sendUserid);
        }
        return null;
    }
    /** Set HTTP headers to prevent caching. */
    private void setNoCacheHeaders(HttpServletResponse aResponse) {
        // Set to expire far in the past.
        aResponse.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");
        
        // Set standard HTTP/1.1 no-cache headers.
        aResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        aResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
        
        // Set standard HTTP/1.0 no-cache header.
        aResponse.setHeader("Pragma", "no-cache");
        
        aResponse.setContentType("text/html;charset=UTF-8");
        
        aResponse.setCharacterEncoding("UTF-8");
        
    }
}
